import { ref, onMounted, onBeforeUnmount, inject } from 'vue'

// 计算表格高度
export default (isHeightAuto: boolean, callback: (p: any) => number) => {
    const tableHeight = ref(0)
    const layoutConfig = inject('layoutConfig')
    const getTableHeight = () => {
        const config: any = layoutConfig
        return window.innerHeight - (callback && typeof callback === 'function' ? callback(config) : 0)
    }
    tableHeight.value = getTableHeight()

    onMounted(() => {
        if ([true, undefined].includes(isHeightAuto)) {
            window.onresize = function() {
                tableHeight.value = getTableHeight();
            }
        }
    })

    onBeforeUnmount(() => {
        if ([true, undefined].includes(isHeightAuto)) {
            window.onresize = null
        }
    })

    return {
        tableHeight
    }
}